package org.jeecg.modules.awards.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.awards.entity.Awards;
import org.jeecg.modules.awards.service.AwardsService;
import org.jeecg.modules.upload.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

/**
 *
 * @author liujiamiao
 * @since 2020-11-05
 */
@Validated
@RestController
@RequestMapping("/awards/awards")
@Api(tags = "获奖情况")
public class AwardsController {

    @Autowired
    private AwardsService awardsService;

    @ApiOperation("分页查询")
    @GetMapping("list/{page}/{size}/{basicId}")
    public Result list(@ApiParam("当前页数")@PathVariable("page")Integer page,
                       @ApiParam("每页大小")@PathVariable("size")Integer size,
                       @ApiParam("基本数据id")@PathVariable("basicId")Integer basicId){
        IPage<Awards> iPage = awardsService.pageSelect(page,size,basicId);
        return Result.ok(iPage);
    }

    @ApiOperation("根据id获取信息")
    @GetMapping("/info/{id}")
    public Result info(@ApiParam("需要删除的id")@PathVariable("id")Integer id){
        Awards awards = awardsService.getById(id);
        return Result.ok(awards);
    }

    @ApiOperation("保存/更新")
    @PostMapping("/save")
    public Result save(@Valid @ApiParam("json数据提交")@RequestBody Awards awards){
        awardsService.saveOrUpdate(awards);
        return Result.ok();
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("/delete/{id}")
    public Result delete(@ApiParam("需要删除的id")@PathVariable("id")Integer id){
        boolean b = awardsService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("上传获奖证书")
    @PostMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile uploadFile) throws IOException {
        String localPath = FileUploadUtils.upload(uploadFile,"images");
        return Result.ok(localPath);
    }

}

